from unittest import TestCase
from Clac import Calc
from data import Data
from ddt import ddt, data, unpack
import xlrd
from xlutils.copy import copy

xl = xlrd.open_workbook(filename='data.xlsx', encoding_override=True)
sh = xl.sheet_by_index(0)
rows = sh.nrows
cols = sh.ncols


@ddt
class TestCalc(TestCase):
    @data(*Data.add)
    @unpack
    def testAdd(self, a, b, c, d):
        calc = Calc()
        sum = calc.add(a, b)
        if sum == c:
            c0 = xlrd.open_workbook('data.xlsx')
            c1 = copy(c0)
            c2 = c1.get_sheet(0)
            c2.write(d, 4, "Pass")
            c1.save('data.xlsx')
        else:
            c0 = xlrd.open_workbook('data.xlsx')
            c1 = copy(c0)
            c2 = c1.get_sheet(0)
            c2.write(d, 4, "Fail")
            c1.save('data.xlsx')
        self.assertEqual(c, sum, "这都能错？")

    @data(*Data.minus)
    @unpack
    def testMinus(self, a, b, c, d):
        calc = Calc()
        sum = calc.minus(a, b)
        if sum == c:
            c0 = xlrd.open_workbook('data.xlsx')
            c1 = copy(c0)
            c2 = c1.get_sheet(0)
            c2.write(d, 4, "Pass")
            c1.save('data.xlsx')
        else:
            c0 = xlrd.open_workbook('data.xlsx')
            c1 = copy(c0)
            c2 = c1.get_sheet(0)
            c2.write(d, 4, "Fail")
            c1.save('data.xlsx')
        self.assertEqual(c, sum, "这都能错？")

    @data(*Data.multi)
    @unpack
    def testMulti(self, a, b, c, d):
        calc = Calc()
        sum = calc.multi(a, b)
        if sum == c:
            c0 = xlrd.open_workbook('data.xlsx')
            c1 = copy(c0)
            c2 = c1.get_sheet(0)
            c2.write(d, 4, "Pass")
            c1.save('data.xlsx')
        else:
            c0 = xlrd.open_workbook('data.xlsx')
            c1 = copy(c0)
            c2 = c1.get_sheet(0)
            c2.write(d, 4, "Fail")
            c1.save('data.xlsx')
        self.assertEqual(c, sum, "这都能错？")

    @data(*Data.devision)
    @unpack
    def testDevision(self, a, b, c, d):
        calc = Calc()
        sum = calc.devision(a, b)
        if sum == c:
            c0 = xlrd.open_workbook('data.xlsx')
            c1 = copy(c0)
            c2 = c1.get_sheet(0)
            c2.write(d, 4, "Pass")
            c1.save('data.xlsx')
        else:
            c0 = xlrd.open_workbook('data.xlsx')
            c1 = copy(c0)
            c2 = c1.get_sheet(0)
            c2.write(d, 4, "Fail")
            c1.save('data.xlsx')
        self.assertEqual(c, sum, "这都能错？")
